package com.explorex.infra.puma.gateway.netty.ws.message;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @author: JacceYang
 * @createTime: 2022/07/15 10:15 上午
 * @description: 自定义消息体头部信息
 */
@Data
public class MessageHeader implements Serializable {
    /**
     * 消息类型
     * @see .MessageTypeEnum
     */
    private short type = -1;
    /**
     * 平台侧分配
     */
    private String appId;
    /**
     * 业务BizId，平台侧分配，分配依据为公司组织的业务线，团队
     */
    @JsonProperty(value = "bizId")
    private Integer bizId;
    /**
     * 子业务类型，平台不做控制，由业务侧自行定义和使用
     */
    @JsonProperty(value = "subBizId")
    private Integer subBizId;
    /**
     * 描述消息体  body 的类型, 包括 json, protobuffer 等，只在sendMessage 发送时需要传递，其他时候不用传
     */
    @JsonProperty(value = "contentType")
    private int contentType;
    /**
     * 链路追踪 id
     */
    @JsonProperty(value = "traceId")
    private String traceId;
    /**
     * 消息应答时 sequence id
     */
    private long seq;
    /**
     * 链接客户端ip地址
     */
    @JsonProperty(value = "remoteIp")
    private String remoteIp;

    /**
     * 关联id
     */
    @JsonProperty(value = "sMid")
    private String sMid;

    /**
     * sdk侧时间
     */
    @JsonProperty(value = "sTime")
    private Long sTime;

    private String action;

    /**
     * 是否单向
     */
    @JsonProperty(value = "oneWay")
    private Boolean oneWay;

    private Long ts;

    @JsonProperty(value = "serviceId")
    private String serviceId;

    private String authorization;

    /**
     * @See QosEnum
     */
    private int qos;

    public MessageHeader() {
    }

    public MessageHeader(short type) {
        this.type = type;
    }

    public void trackHeader(MessageHeader req) {
        this.traceId = req.getTraceId();
        this.seq = req.getSeq();
        this.appId = req.getAppId();
        this.sTime = req.getSTime();
        this.bizId = req.getBizId();
        this.subBizId = req.getSubBizId();
        this.contentType = req.getContentType();
        this.sMid = req.getSMid();
        this.action = req.getAction();
        this.ts = System.currentTimeMillis();
        this.oneWay = req.getOneWay();
    }
}
